<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增员工')" />
    <th:block th:include="include :: layout-latest-css" />
    <th:block th:include="include :: select2-css" />
</head>
<body>
<div class="main-content">
    <form id="form-employee-add" class="form-horizontal" method="post" action="">
        <input name="deptId" type="hidden" id="treeId"/>
        <h4 class="form-header h4">基本信息</h4>
        <div class="row ui-layout-center">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">员工名称：</label>
                    <div class="col-sm-8">
                        <input name="name" placeholder="请输入用户名称"autocomplete="off" class="form-control" type="text" maxlength="30">
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">归属部门：</label>
                    <div class="col-sm-8">
                        <div class="input-group">
                            <input name="deptName" onclick="selectDeptTree()" autocomplete="off" id="treeName" type="text" placeholder="请选择归属部门" class="form-control">
                            <span class="input-group-addon"><i class="fa fa-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">手机号码：</label>
                    <div class="col-sm-8">
                        <div class="input-group">
                            <input id="mobile" name="mobile" autocomplete="off" type="number" placeholder="请输入手机号码" class="form-control" maxlength="11">
                            <span class="input-group-addon"><i class="fa fa-mobile"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">员工性别：</label>
                    <div class="col-sm-8">
                        <div class="input-group">
                            <select id="gender" name="gender" class="form-control" type="text">
                                <option value="1">男</option>
                                <option value="0">女</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">入职日期：</label>
                    <div class="col-sm-8">
                        <input type="text" class="time-input"autocomplete="off" id="fromDate" placeholder="日期" name="fromDate"/>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">员工编号：</label>
                    <div class="col-sm-8">
                        <input name="empNo" id="empNo" placeholder="员工编号" autocomplete="off" class="form-control" type="number" maxlength="30">
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-xs-2 control-label">岗位：</label>
                    <div class="col-xs-4">
                        <select id="post" class="form-control select2-multiple" multiple>
                            <option th:each="post:${posts}" th:value="${post.postId}" th:text="${post.postName}" th:disabled="${post.status == '1'}"></option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <h4 class="form-header h4">其他信息</h4>
        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-xs-2 control-label">备注：</label>
                    <div class="col-xs-10">
                        <textarea name="remark" maxlength="500" class="form-control" rows="3"></textarea>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-offset-5 col-sm-10">
                <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
                <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
            </div>
        </div>
    </form>
</div>

<!--<div class="row">-->
<!--    <div class="col-sm-offset-5 col-sm-10">-->
<!--        <button type="submit" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;-->
<!--        <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>-->
<!--    </div>-->
<!--</div>-->
<th:block th:include="include :: footer" />
<th:block th:include="include :: select2-js" />
<script>
    var prefix = ctx + "system/employee";

    $("#mobile").blur( function checkPhone(){
        var phone = $("#mobile").val();
        if(!(/^1[3456789]\d{9}$/.test(phone))){
            alert("手机号码不合法，请重新输入");
            $(this).val($(this).attr(" "));
            return false;
        }
    })



    $("#form-employee-add").validate({
        onkeyup: false,
        rules: {
            loginName: {
                minlength: 2,
                maxlength: 20,
                remote: {
                    url: prefix + "/checkLoginNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "loginName": function () {
                            return $.common.trim($("#loginName").val());
                        }
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            password: {
                minlength: 5,
                maxlength: 20
            },
            email: {
                required: true,
                email: true,
                remote: {
                    url: prefix + "/checkEmailUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "email": function () {
                            return $.common.trim($("#email").val());
                        }
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            mobile: {
                required: true,
                isPhone: true,
                remote: {
                    url: prefix + "/checkPhoneUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "phonenumber": function () {
                            return $.common.trim($("#mobile").val());
                        }
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);

                    },
                }
            },
            empNo: {
                required: true,
                remote: {
                    url: prefix + "/checkEmpNoUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "empNo": function () {
                            return $.common.trim($("#empNo").val());
                        }
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
        },
        messages: {
            "loginName": {
                remote: "用户已经存在"
            },
            "email": {
                remote: "Email已经存在"
            },
            "mobile": {
                remote: "手机号码已经存在"
            },
            "empNo": {
                remote: "员工号已经存在"
            }
        },
        focusCleanup: true,


    })

    // $.validator.setDefaults({
    //     submitHandler:  function() {
    //         var data = $("#form-employee-add").serializeArray();
    //         var postIds = $.form.selectSelects("post");
    //         data.push({"name": "postIds", "value": postIds});
    //         $.operate.saveTab(prefix + "/add", data);
    //     }
    // });

    function submitHandler() {
        var data = $("#form-employee-add").serializeArray();
        var postIds = $.form.selectSelects("post");
        data.push({"name": "postIds", "value": postIds});
        $.operate.saveTab(prefix + "/add", data);
    }

    /* 用户管理-新增-选择部门树 */
    function selectDeptTree() {
        var treeId = $("#treeId").val();

        var deptId = $.common.isEmpty(treeId) ? "1" : $("#treeId").val();
        var url = ctx + "system/dept/selectDeptTree/" + deptId;
        var options = {
            title: '选择部门',
            width: "380",
            url: url,
            callBack: doSubmit
        };
        console.log(1)
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero){
        var tree = layero.find("iframe")[0].contentWindow.$._tree;
        var body = layer.getChildFrame('body', index);
        $("#treeId").val(body.find('#treeId').val());
        $("#treeName").val(body.find('#treeName').val());
        layer.close(index);
    }

    $(function() {
        $('#post').select2({
            placeholder: "请选择岗位",
            allowClear: true
        });
    })
</script>
</body>
</html>